package com.op.teachingliteapp.fragment;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.op.teachingliteapp.R;
import com.op.teachingliteapp.common.Common;
import com.op.teachingliteapp.common.api.teachinglite.NotifUrl;
import com.op.teachingliteapp.fragment.notif.NotifFragment;
import com.op.teachingliteapp.util.asynctask.HttpPostTask;
import com.op.teachingliteapp.util.asynctask.OnTaskProcessListener;

/**
 * 
 * @author handsome
 *
 */
public class SendNotifFragment extends Fragment {
	private EditText titleEditText;
	private EditText contentEditText;
	private String getCourseId;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View root = inflater.inflate(R.layout.fragment_send_notif, container, false);

		Button send_message = (Button) root.findViewById(R.id.send_notif_button);
		titleEditText = (EditText) root.findViewById(R.id.notif_title);
		contentEditText = (EditText) root.findViewById(R.id.notif_content);
		
		SharedPreferences preferences = getActivity().getSharedPreferences("TeachingLite", Context.MODE_PRIVATE);
		getCourseId=preferences.getString(Common.Prefs.COURSE_ID, "");
		Log.i("SendNotifFragment", getCourseId+"");
		send_message.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String title = titleEditText.getText().toString();
				String content = contentEditText.getText().toString();
				if (title.length() > 0 && content.length() > 0) {
					sureSend();
				} else {
					AlertDialog.Builder builder = new Builder(getActivity());
					builder.setMessage("标题和内容不能为空");
					builder.setTitle("提示");
					builder.setNegativeButton("确定", null);
					builder.create();
					builder.show();
				}
			}
		});
		return root;
	}

	/**
	 * 确认发送通知
	 */
	public void sureSend() {
		AlertDialog.Builder builder = new Builder(getActivity());
		builder.setMessage("确认发送通知吗？");
		builder.setTitle("提示");
		builder.setNeutralButton("确认", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				String title = titleEditText.getText().toString();
				String content = contentEditText.getText().toString();
				sendToOnlineData(title, content, getCourseId);
				switchFragment(new NotifFragment());
			}
		});
		builder.setNegativeButton("取消", null);
		builder.create();
		builder.show();
	}

	/**
	 * 发送字符串到网上数据库title,content,courseId,userId 依次是标题，内容，课程id,用户ID
	 */
	public void sendToOnlineData(String title, String Content, String courseId) {
		SharedPreferences preferences = getActivity().getSharedPreferences("TeachingLite", Context.MODE_PRIVATE);
		String getUserId = preferences.getString(Common.Prefs.USER_ID, "");// 用户id
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair(Common.Notif.ADD_COURSE_NOTIF_CONTENT, Content));
		params.add(new BasicNameValuePair(Common.Notif.ADD_COURSE_NOTIF_USER_ID, getUserId));
		params.add(new BasicNameValuePair(Common.Notif.ADD_COURSE_NOTIF_TITLE, title));
		params.add(new BasicNameValuePair(Common.Notif.ADD_COURSE_NOTIF_COURSE_ID, courseId));

		HttpPostTask task = new HttpPostTask(getActivity(), NotifUrl.ADD_COURSE_NOTIF, params);
		task.setTaskListener(new OnTaskProcessListener() {

			@Override
			public void prepare() {

			}

			@Override
			public void completed(String result) {
				// ToastUtils.show(getActivity(), "发布通知成功");
			}
		});
		task.execute();
	}

	/**
	 * 清空堆栈再添加fragment
	 */
	@SuppressWarnings("unused")
	private void switchFragment(Fragment fragment) {
		getFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);// 清空堆栈
		getFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.main_show2, fragment).commit();

	}
}
